package com.woniu.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.job.entity.Favorite;
import com.woniu.job.entity.Interview;
import com.woniu.job.entity.Resumework;
import com.woniu.job.entity.Work;
import com.woniu.job.exception.service.NoDefaultResourceException;
import com.woniu.job.exception.service.ResumeWorkIsExistException;
import com.woniu.job.mapper.FavoriteMapper;
import com.woniu.job.mapper.InterviewMapper;
import com.woniu.job.mapper.ResumeworkMapper;
import com.woniu.job.mapper.WorkMapper;
import com.woniu.job.service.IFavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.job.util.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author song
 * @since 2019-12-24
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {
    @Resource
    private FavoriteMapper favoriteMapper;
    @Resource
    private WorkMapper workMapper;
    @Resource
    private ResumeworkMapper resumeworkMapper;
    @Autowired
    private InterviewMapper interviewMapper;

    /**
     * @Description: 用户增加收藏职位
     * @Param: [favorite]
     * @return: void
     * @Author: 陈C
     * @Date: 2019/12/26
     */
    @Override
    public void favoriteAdd(Favorite favorite) {
        favoriteMapper.insert(favorite);
    }

    /**
     * @Description: 查看我的职位收藏
     * @Param: [id]
     * @return: void
     * @Author: 陈C
     * @Date: 2019/12/26
     */
    @Override
    public List<Work> getAllFavo(String id) {
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", id);
        //得到所有的职位id
        List<Favorite> wId = favoriteMapper.selectList(queryWrapper);
        List<Work> workList = new ArrayList<>();
        //根据职位id查询所有职位
        wId.forEach(s -> workList.add(workMapper.selectById(s)));
        //返回用户所有职位
        return workList;
    }

    /**
     * @Description: 删除职位收藏
     * @Param: [wordId]
     * @return: void
     * @Author: 陈C
     * @Date: 2019/12/26
     */
    @Override
    public void delFavo(String[] wordId) {
        for (int i = 0; i < wordId.length; i++) {
            QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("wordId", wordId[i]);
            favoriteMapper.delete(queryWrapper);
        }


    }
    /**
    * @Description: 投递简历
    * @Param: [用户id, 工作id1]
    * @return: void
    * @Author: 陈C
    * @Date: 2019/12/28
    */
    @Override
    public void sendResdResume(String id, String id1) {
        //根据用户id查询设为默认的简历id
        if (favoriteMapper.getByIdAndStatus(id)==null){
            throw  new NoDefaultResourceException("没有默认简历");
        }
        String resumeId = favoriteMapper.getByIdAndStatus(id).getId();
        //判断简历是否重复添加
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("workId",id1);
        queryWrapper.eq("resumeId",resumeId);
        if(resumeworkMapper.selectList(queryWrapper).size()==0){
            //增加
            Resumework resumework = new Resumework();
            resumework.setId(Helper.getId());
            resumework.setSendTime(Helper.getNowTime());
            resumework.setWorkId(id1);
            resumework.setResumeId(resumeId);
            resumework.setStatus("1");
            resumeworkMapper.insert(resumework);
        }else{
            throw new ResumeWorkIsExistException("已经投递");
            }
        }
    /**
    * @Description: 查看面试邀请
    * @Param: [id]
    * @return: java.util.List<com.woniu.job.entity.Interview>
    * @Author: 陈C
    * @Date: 2019/12/30
    */
    @Override
    public List<Interview> lookResume(String id) {
        QueryWrapper<Interview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", id);
        return interviewMapper.selectList(queryWrapper);
    }

}
